/***
 *	Shop Product JavaScriptShop
 **/


$().ready( function() {

	$productListForm = $("#productListForm");
	$pageNumber = $("#pageNumber");
	$pageSize = $("#pageSize");
	$orderType = $("#orderType");
	$viewType = $("#viewType");
	$tableType = $("#tableType");
	$pictureType = $("#pictureType");
	$addFavorite = $(".addFavorite");
	$addCartItem = $(".addCartItem");
	$addCartItemTip = $("#addCartItemTip");
	$tipClose = $("#addCartItemTip .tipClose");
	$addCartItemTipMessageIcon = $("#addCartItemTipMessageIcon");
	$addCartItemTipMessage = $("#addCartItemTipMessage");
	$addCartItemTipInfo = $("#addCartItemTipInfo");

	// 添加商品至购物车
	$addCartItem.click( function() {
		var $this = $(this);
		var id = $this.metadata().id;
		console.log("id-----"+id);
		console.log($this.html());
		var shopId=$this.metadata().shopId;
		var returnUrl = $this.metadata().returnUrl;
		var x = $this.offset().left - 50;
		var y = $this.offset().top + $this.height() + 6;
		$addCartItemTip.css({"left" :x, "top" :y});
		
		var quantity = $("#quantity").val();
		if (quantity == null) {
			quantity = 1;
		}

		//商品规格
		var isSelect = true;
		var specificationValueIds = "";
		var selections = $(".nature-container");
		if (selections.length>=1) {
            $.each($(".nature-container"), function (k, v) {
                var select = $(this).children().children().find('.a-item.J_ping.selected');
                if (select.length<1){
                    isSelect = false;
                    layer.alert("请选择"+$(this).attr("specificationName"));
                    return false;
                }else {
                    specificationValueIds+=select.attr("specificationValueId")+",";
                }
            });
            if (!isSelect){
                return false;
            }
        }

		var reg = /^[0-9]*[1-9][0-9]*$/;
		if (!reg.test(quantity)) {
            layer.alert("商品数量必须为正整数");
			return false;
		}
		$.ajax({
			url: "/b/cartItem/ajaxAdd",
			data: {"id": id, "quantity": quantity,"returnUrl": returnUrl,"shopId":shopId,"specificationValueIds":specificationValueIds},
			dataType: "json",
			beforeSend: function() {
				$this.attr("disabled", true);
			},
			success: function(data) {
				/*$.flushCartItemList();*/
				if (data.status == "success") {
					if(data.returnUrl!="" && data.returnUrl!=undefined){
						window.location.href=data.returnUrl
					}else{
						$("#ECS_CARTINFO").html(data.totalQuantity);
						layer.msg(data.message,{time:500},function () {
                            $(".flick-menu-mask").hide();
                            $(".spec-menu").removeClass("modal-active");
                            $("body").removeClass("hidden");
						});
					}

				} else if (data.status == "error") {
					layer.open({
						content: data.message
						,btn: '确定'
					});
					//alert(data.message);
					/*$addCartItemTipMessageIcon.removeClass("successIcon").addClass("errorIcon");
					 $addCartItemTipMessage.text(data.message);
					 $addCartItemTipInfo.empty();*/
				}
				$addCartItemTip.fadeIn();
				$this.attr("disabled", false);
			}
		});
	});

	// 添加购物车提示框隐藏
	$tipClose.click( function() {
		$addCartItemTip.fadeOut();
		return false;
	});

	// 产品收藏
	$addFavorite.click( function() {
		var $this = $(this);
		if ($.cookie("loginMemberUsername") == null) {
			$.flushHeaderInfo();
			$.loginWindowShow();
			return false;
		} else {
			var id = $(this).metadata().id;
			$.ajax({
				url: JFinalshop.base + "/shop/favorite/ajaxAdd",
				data: {"id": id},
				dataType: "json",
				beforeSend: function() {
					$this.attr("disabled", true);
				},
				success: function(data) {
					$.tip(data.status, data.message);
					$this.attr("disabled", false);
				},
				error: function(data) {
					if ($.cookie("loginMemberUsername") == null) {
						$.flushHeaderInfo();
						$.loginWindowShow();
						return false;
					}
					$this.attr("disabled", false);
				}
			});
		}
	});


});